In Class Exercise 4

Author

Hao Xian

Published

January 30, 2023

Modified

February 6, 2023

1 Loading the package

pacman::p_load(maptools, sf, raster, spatstat, tmap)

Things to learn outside from this code chunk.

2 Load Dataset

childcare_sf <- st_read("data/geospatial/child-care-services-geojson.geojson") %>%
  st_transform(crs = 3414)
Reading layer `child-care-services-geojson' from data source 
  `C:\hxchen-2019\birdie\lessons\In-class\in-class_ex4\data\geospatial\child-care-services-geojson.geojson' 
  using driver `GeoJSON'
Simple feature collection with 1545 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6824 ymin: 1.248403 xmax: 103.9897 ymax: 1.462134
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84

This is already a projected coordinate system. This is converted to meters.

Note

For all spatial analysis, all data must be projected coordinate system.

sg_sf <- st_read(dsn = "data/geospatial", layer="CostalOutline")
Reading layer `CostalOutline' from data source 
  `C:\hxchen-2019\birdie\lessons\In-class\in-class_ex4\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 60 features and 4 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 2663.926 ymin: 16357.98 xmax: 56047.79 ymax: 50244.03
Projected CRS: SVY21
mpsz_sf <- st_read(dsn = "data/geospatial", 
                layer = "MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `C:\hxchen-2019\birdie\lessons\In-class\in-class_ex4\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21
Note

tmap_mode(“plot”) will give a static map

tmap_mode(“view”) will give a interactive map

tmap is an extension of leaflet

alpha is to introduce a certain level of opacity

tm_shape is needed to create a new layer. It needs to be at the start of everyplot

tmap_mode('view')
tm_shape(childcare_sf) +
  tm_dots(alph = 0.5, size=0.01) 

2.0.1 Converting to Spatial Dataframe

Note

Spatial Polygon has a data table

childcare <- as_Spatial(childcare_sf)
mpsz <- as_Spatial(mpsz_sf)
sg <- as_Spatial(sg_sf)

2.0.2 Converting to Spatial Object

Note

This only retains the geometric

childcare_sp <- as(childcare, "SpatialPoints")
sg_sp <- as(sg, "SpatialPolygons")

2.0.3 Converting to PPP format

Note

This drops everything else into a list of coordinates that can only be read by the ST package

childcare_ppp <- as(childcare_sp, "ppp")
childcare_ppp
Planar point pattern: 1545 points
window: rectangle = [11203.01, 45404.24] x [25667.6, 49300.88] units

3 Additional Map

tmap_mode('view')
tm_shape(childcare_sf) +
  tm_dots(alph = 0.5, size=0.01) +
  tm_view(set.zoom.limits = c(11,14), set.bounds = TRUE)